package org.tong.validate;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.BindException;
import java.util.stream.Collectors;

/**
 * @author
 * @date 2022-08-26 9:33
 */
@Component
@ControllerAdvice
@Slf4j
public class MyControllerAdvice {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Object bindException(MethodArgumentNotValidException bindException) {
        log.info("校验拦截");
        return bindException.getBindingResult().getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("-"));
    }
}
